package com.bop.dehong.base;

import java.util.List;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.base.dehong.R;
import com.base.dehong.util.ToastUtils;
import com.bop.dehong.dialog.NetworkErrorDialog;
import com.bop.dehong.util.LoadingAnimation;
import com.umeng.analytics.MobclickAgent;

/**
 * @author YeFengyun
 * 
 * @describe FragmentActivity基类
 */
@SuppressLint("NewApi")
public abstract class BaseFragmentActivity extends FragmentActivity  implements OnClickListener,OnItemClickListener{
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
	 */
	@TargetApi(Build.VERSION_CODES.GINGERBREAD)
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
 		super.onCreate(savedInstanceState);
 		// 添加Activity到堆栈
		ActManager.getAppManager().addActivity(this);
		setContentView(getContentViewId());
		initTop();
		initUI();
		initData();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// 结束Activity&从堆栈中移除
		ActManager.getAppManager().finishActivity(this);
		NetworkErrorDialog.getInstance().dismiss();
	}
 

	public static Activity getCurrentActivity(){
		return ActManager.getAppManager().getTopActivity();
	}
 

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
//		MobclickAgent.onPageStart(TAG);//应用程序含Fragment不能在FragmentActivity中调用onPageStart、onPageEnd方法
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
//		MobclickAgent.onPageEnd(TAG);
		MobclickAgent.onPause(this);
		ToastUtils.hideToast();
	}
	
	@Override
	protected void onStart() {
		super.onStart();
	}
	
	@Override
	protected void onStop() {
		super.onStop();
	}
	
	/**
	 * 通过类名启动Activity
	 * 
	 * @param pClass
	 */
	protected void openActivity(Class<?> pClass) {
		openActivity(pClass, null);
	}

	/**
	 * 通过类名启动Activity，并且含有Bundle数据
	 * 
	 * @param pClass
	 * @param pBundle
	 */
	protected void openActivity(Class<?> pClass, Bundle pBundle) {
		Intent intent = new Intent(this, pClass);
		if (pBundle != null) {
			intent.putExtras(pBundle);
		}
		startActivity(intent);
	}

	/**
	 * 通过Action启动Activity
	 * 
	 * @param pAction
	 */
	protected void openActivity(String pAction) {
		openActivity(pAction, null);
	}

	/**
	 * 通过Action启动Activity，并且含有Bundle数据
	 * 
	 * @param pAction
	 * @param pBundle
	 */
	protected void openActivity(String pAction, Bundle pBundle) {
		Intent intent = new Intent(pAction);
		if (pBundle != null) {
			intent.putExtras(pBundle);
		}
		startActivity(intent);
	}
	public void onTitleLeftClick(View v){
		finishCurrentActivity();
	}
 
    /**
     * 程序是否在前台运行
     * 
     * @return
     */
    public boolean isAppOnForeground() {
        // Returns a list of application processes that are running on the device
         
        ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        String packageName = getApplicationContext().getPackageName();

        List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        if (appProcesses == null) {
            return false;
        }

        for (RunningAppProcessInfo appProcess : appProcesses) {
            // The name of the process that this object is associated with.
            if (appProcess.processName.equals(packageName) && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
            	return true;
            }
        }

        return false;
    }
    
    public boolean isScreenLocked() {
        KeyguardManager mKeyguardManager = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
        return mKeyguardManager.inKeyguardRestrictedInputMode();
    }
	


	/**
	 * 结束当前Activity
	 * 
	 * @param pClass
	 */
	protected void finishCurrentActivity() {
		getCurrentActivity().finish();
	}

    
	public void finishForDelay() {

		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				getCurrentActivity().finish();
			}
		}, 1000);
	}
	/**
	 * 初始化标题,最新处理
	 */
	protected abstract void initTop();
	/**
	 * 初始化UI，，第二处理
	 */
	protected abstract void initUI();
	/**
	 * 初始化数据，，第三处理
	 */
	protected abstract void initData();
	/**
	 * 设置ContentView
	 * @return
	 */
	protected abstract int getContentViewId();
	
	public abstract int getCourrentPosition();

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		 if (keyCode == KeyEvent.KEYCODE_BACK) {
			// 结束Activity&从堆栈中移除
			ActManager.getAppManager().finishActivity(this);
			return true;
		 }
		return super.onKeyDown(keyCode, event);
	}
	LoadingAnimation an;
	protected  void showProgressDialog(){
		if (an == null) {
			an = new LoadingAnimation(this);
		}
		an.createAnimation();
	}
	
	protected  void disappearProgressDialog(){
		if (an!= null) {
			an.dismissAnimation();
		}
	}
	/**
	  * 从控件中获取文本
	  * @param view
	  * @return
	  * 作者： tangwenhan
	  * 时间 ：下午2:59:23
	  */
	 public String getStr4TextView(TextView view){
		 return view.getText().toString().trim();
	 }
	
	public final <E extends View> E getView(int id) {
		try {
			return (E) findViewById(id);
		} catch (ClassCastException ex) {
			Log.e("界面初始化化控件", "类转型异常", ex);
			throw ex;
		}
	}

	public final <E extends View> E getView(View parent, int id) {
		try {
			return (E) parent.findViewById(id);
		} catch (ClassCastException ex) {
			Log.e("通过父类布局初始化化控件", "类转型异常", ex);
			;
			throw ex;
		}
	}
	/**
	 * 注册View的点击事件,然后重写onClick接口函数
	 * @param views
	 */
	public void registerOnClickListener(View...views) {
		for (int i = 0; i < views.length; i++) {
			views[i].setOnClickListener(this);
		}
	}
	public void showNetErrorDialog(){
		
		NetworkErrorDialog.getInstance().show(this);
	}
	public void finish()
	{
		super.finish();
		//Activity退出的效果
		overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
	
}
